package com.zfjt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.BrabchBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.packnum.PackNum00007;
import com.zfjt.service.BrabchService;
import com.zfjt.utils.StringUtil;

/**
 * 增加或删除网点（权限为0才能删除网点）
 * @author xk
 * @version 1.0
 * @date 2017年3月30日
 */
@Controller
public class AddBrabchController {
	
	private ReturnBean mReturn;
	
	@Autowired
	private BrabchService mBrabch;
	
	private Map<String, String> fhdata;
	
	private int is = 0;
	
	@RequestMapping(value="00007")
	@ResponseBody
	public ReturnBean AddBrabch(HttpSession hseesion){
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00007 num00007 = JSON.parseObject(data,PackNum00007.class); 		
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00007");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{
			List<BrabchBean> mData = mBrabch.selectAll();
			for (int i = 0; i < mData.size(); i++) {//验证数据库中有无此网点
				if(mData.get(i).getBrabchName().equals(num00007.getBrabchname())){
					is = 1;
				}
			}
			if(is == 1){
				mReturn.setCode("002");
				mReturn.setPortnum("00007");
				fhdata.put("error", "BRABCH_ERROR");//数据库已有此网点
				mReturn.setData(JSON.toJSONString(fhdata));
			}else{
				BrabchBean brabch = new BrabchBean();
				String[] str = StringUtil.convertStrToArray2(UUID.randomUUID().toString());
				brabch.setBrabchId(str[4]);
				brabch.setBrabchName(num00007.getBrabchname());
				brabch.setSubordinate(num00007.getAccount());
				brabch.setAffiliation("0");
				mBrabch.insert(brabch);
				mReturn.setCode("000");
				mReturn.setPortnum("00007");
				mReturn.setData("添加成功");
			}
		}		
		return mReturn;
	}
}
